package com.atfcm.hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class T_219_存在重复元素 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            List<Integer> indexList = map.get(nums[i]);
            if(indexList == null) {
                indexList = new ArrayList<>();
                indexList.add(i);
                map.put(nums[i], indexList);
            }else {
                indexList = new ArrayList<>();
                for (Integer integer : indexList) {
                    if(Math.abs(i - integer) <= k) {
                        return true;
                    }
                }
                indexList.add(i);
            }
        }
        return false;
    }
}
